<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\admin\model;
use think\Model;
use think\facade\Session;
class UserLogModel extends Model{
    protected $name = 'user_log';    
    protected $pk = 'id';    
    //增加登录日志
    public function add($type,$table_name,$primary_key=0,$remark=null){
        if(is_array($remark)){
            $remark= json_encode($remark,JSON_UNESCAPED_UNICODE);
        }
        $data=array();
        $data["userid"]=Session::get("user_id");
        $data["username"]=Session::get("username");
        $data["addtime"]=time();
        $data["nodestr"]=admin_nav();
        $data["type"]=$type;
        $data["url"]=$this->GetCurUrl();
        $data["ip"]=  get_client_ip();
        $data["table_name"]=$table_name;
        //这里会自动判断$primary_key是array还是int形式
        if(is_array($primary_key)){
            $data["primary_key"]=implode(",", $primary_key);
        }else{
            $data["primary_key"]=$primary_key;
        }
        $data["remark"]=$remark;
        $this->create($data);
    }
    //获取当前访问的url
    public function GetCurUrl() {
        $url = 'http://';
        if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
            $url = 'https://';
        }

        // 判断端口
        if($_SERVER['SERVER_PORT'] != '80') {
            $url .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . ':' . $_SERVER['REQUEST_URI'];
        } else {
            $url .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['REQUEST_URI'];
        }

        return $url;
    }    
}
